<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="一年春又来">
<meta property="og:url" content="http://littlefxc.github.io/page/6/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="一年春又来">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://littlefxc.github.io/page/6/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'zh-CN'
  };
</script>
<title>一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/09/docs/JVM/JVM%E4%B9%8B%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/09/docs/JVM/JVM%E4%B9%8B%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/" class="post-title-link" itemprop="url">JVM之对象的创建过程</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-09 17:11:23" itemprop="dateCreated datePublished" datetime="2021-07-09T17:11:23+08:00">2021-07-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:25:12" itemprop="dateModified" datetime="2022-05-10T09:25:12+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/isn4Ur.png" alt="image-20210709171519463"></p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/07/09/docs/JVM/JVM%E4%B9%8B%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%88%9B%E5%BB%BA%E8%BF%87%E7%A8%8B/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/08/Keepalived%E5%8E%9F%E7%90%86%E5%92%8C%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/08/Keepalived%E5%8E%9F%E7%90%86%E5%92%8C%E9%85%8D%E7%BD%AE%E8%AF%A6%E8%A7%A3/" class="post-title-link" itemprop="url">Keepalived原理和配置详解</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-07-08 15:50:31 / 修改时间：16:26:12" itemprop="dateCreated datePublished" datetime="2021-07-08T15:50:31+08:00">2021-07-08</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-Keepalived-简介"><a href="#1-Keepalived-简介" class="headerlink" title="1 Keepalived 简介"></a>1 Keepalived 简介</h1><p>Keepalived 软件起初是专为 LVS 负载均衡软件设计的，用来管理并监控 LVS 集群系统中各个服务节点的状态，后来又加入了可以实现高可用的 VRRP 功能。因此，Keepalived除了能够管理 LVS 软件外，还可以作为其他服务（例如：Nginx、Haproxy、MySQL等）的高可用解决方案软件。</p>
<p>Keepalived 软件主要通过 VRRP 协议实现高可用功能的，VRRP 是 Virtual Router Redundancy Protocol （虚拟路由器冗余协议）的缩写，VRRP 出现的目的就是为了解决动态路由单点故障问题的，它能够保证当个别节点宕机时，整个网络可以不间断的运行。所以，Keepalived 一方面具有配置管理 LVS 的功能，同时还具有对LVS 下面节点进行健康检查的功能，另一方面也可以实现系统网络服务的高可用功能。</p>
<p>Keepalived  软件的官方站点： <a target="_blank" rel="noopener" href="http://www.keepalived.org/">http://www.keepalived.org</a></p>
<h1 id="2-Keepalived-服务的三个重要功能"><a href="#2-Keepalived-服务的三个重要功能" class="headerlink" title="2 Keepalived 服务的三个重要功能"></a>2 Keepalived 服务的三个重要功能</h1><h2 id="2-1-管理-LVS-负载均衡软件"><a href="#2-1-管理-LVS-负载均衡软件" class="headerlink" title="2.1 管理 LVS 负载均衡软件"></a>2.1 管理 LVS 负载均衡软件</h2><p>早期的 LVS 软件，需要通过命令行或脚本实现管理，并且没有针对 LVS 节点的健康检查功能。为了解决 LVS 的这些使用不便的问题，Keepalived就诞生了，可以说，Keepalived软件起初是专为了解决 LVS 的问题而诞生的。因此，Keepalived和LVS的感情很深，它们的关系如同夫妻一样，可以紧密的结合，愉快的工作。Keepalived 可以通过读取自身的配置文件实现通过更底层的接口直接管理 LVS 的配置以及控制服务的启动、停止等功能，这使得 LVS 的应用就更加简单方便了。</p>
<h2 id="2-2-实现对-LVS-集群节点健康检查功能（healthcheck）"><a href="#2-2-实现对-LVS-集群节点健康检查功能（healthcheck）" class="headerlink" title="2.2 实现对 LVS 集群节点健康检查功能（healthcheck）"></a>2.2 实现对 LVS 集群节点健康检查功能（healthcheck）</h2><p>Keepalived 可以通过在自身的keepalived.conf文件里配置 LVS 的节点 IP 和相关参数实现对 LVS 的直接管理；除此之外，当 LVS 集群中的某一个甚至是几个节点服务器同时发生故障无法提供服务时，Keepalived 服务会自动将失效的节点服务器从 LVS 的正常转发队列中清楚出去，并转换到别的正常节点服务器上，从而保证最终用户的访问不受影响；当故障的节点服务器被修复后，Keepalived 服务又会自动地把它们加入到正常转发队列中，对客户提供服务。</p>
<h2 id="2-3作为系统网络服务的高可用功能（failover）"><a href="#2-3作为系统网络服务的高可用功能（failover）" class="headerlink" title="2.3作为系统网络服务的高可用功能（failover）"></a>2.3作为系统网络服务的高可用功能（failover）</h2><p>Keepalived 可以实现任意两台主机之间，例如 Master 和 Backup 主机之间的故障转移和自动切换，这个主机可以是普通的不能停机的业务服务器，也可以是 LVS 负载均衡、Nginx 反向代理这样的服务器。</p>
<p>Keepalived 高可用功能实现的原理为：两台主机同时安装好 keepalived 软件并启动服务，开始正常工作时，由角色为 Master 的主机获得所有资源并对用户提供服务，角色 Backup 的主机作为 Master 主机的热备；当角色为 Master 的主机失效或出现故障时，角色为 Backup 的主机将自动接管 Master 主机的所有工作，包括接管 VIP 资源及相应资源服务；而当角色为 Master 的主机故障修复后，又会自动接管回它原来处理的工作，角色为 Backup 的主机则同时释放 Master 主机失效它接管的工作，此时，两台主机将恢复到最初启动时各自的原始角色及工作状态。</p>
<h1 id="3-运行原理"><a href="#3-运行原理" class="headerlink" title="3 运行原理"></a>3 运行原理</h1><p>Keepalived 高可用服务对之间的故障切换转移，是通过 VRRP 协议（虚拟路由冗余协议）来实现的。</p>
<p>在 Keepalived 服务正常工作时，主 Master 节点会不断地向备节点发送（多播的方式）心跳消息，用以告诉备 Backup 节点自己还活着，当主 Master 节点发生故障时，就无法发送心跳消息了，备节点也就因此无法继续检测到来自Master 节点的心跳了，进而调用自身的接管程序，接管主 Master 节点的 IP 资源及服务。而当主 Master 节点恢复时，备 Backup 节点又会释放主节点故障时自身接管的 IP 资源及服务，恢复到原来备用角色。</p>
<h1 id="4-选举策略"><a href="#4-选举策略" class="headerlink" title="4 选举策略"></a>4 选举策略</h1><p>选举策略是根据 VRRP 协议，完全按照权重大小，权重最大（0～255）的是 MASTER 机器，下面几种情况会触发选举</p>
<ol>
<li>keepalived 启动的时候</li>
<li>master 服务器出现故障（断网，重启，或者本机器上的 keepalived crash 等，而本机器上其他应用程序 crash 不算）</li>
<li>有新的备份服务器加入且权重最大</li>
</ol>
<h1 id="5-VRRP协议"><a href="#5-VRRP协议" class="headerlink" title="5 VRRP协议"></a>5 VRRP协议</h1><p>VRRP 协议，全称 Virtual Router Redundancy Protocol，中文名为虚拟路由冗余协议，VRRP 的出现就是为了解决静态路由的单点故障问题，VRRP 协议是通过一种竞选机制来将路由的任务交给某台 VRRP 路由器的。VRRP 协议早期是用来解决交换机、路由器等设备单点故障的。</p>
<h2 id="5-1-VRRP-原理描述（同样适用于-Keepalived-的工作原理）"><a href="#5-1-VRRP-原理描述（同样适用于-Keepalived-的工作原理）" class="headerlink" title="5.1 VRRP 原理描述（同样适用于 Keepalived 的工作原理）"></a>5.1 VRRP 原理描述（同样适用于 Keepalived 的工作原理）</h2><p>在一组 VRRP 路由器集群中，有多台物理 VRRP 路由器，但是这多台物理的机器并不是同时工作的，而是由一台称为 MASTER 的机器负责路由工作，其他的机器都是 BACKUP。MASTER 角色并非一成不变，VRRP 协议会让每个 VRRP 路由参与竞选，最终获胜的就是 MASTER。MASTER 拥有虚拟路由器的 IP 地址，我们把这个 IP 地址称为 VIP，MASTER 负责转发发送给网关地址的数据包和响应 ARP 请求。</p>
<h2 id="5-2-VRRP-是如何工作的？"><a href="#5-2-VRRP-是如何工作的？" class="headerlink" title="5.2 VRRP 是如何工作的？"></a>5.2 VRRP 是如何工作的？</h2><p>VRRP 协议通过竞选机制来实现虚拟路由器的功能，所有的协议报文都是通过 IP 多播（默认的多播地址：224.0.0.18）形式进行发送。虚拟路由器由 VRID （范围0-255）和一组 IP 地址组成，对外表现为一个周知的 MAC 地址：00-00-5E-00-01-{VRID}。所以，在一个虚拟路由器中，不管谁是 MASTER，对外都是相同的 MAC 地址和 IP 地址，如果其中一台虚拟路由器宕机，角色发生切换，那么客户端并不需要因为 MASTER 的变化修改自己的路由设置，可以做到透明的切换。这样就实现了如果一台机器宕机，那么备用的机器会拥有 MASTER 上的 IP 地址，实现高可用功能。</p>
<h2 id="5-3-VRRP-是如何通信的？"><a href="#5-3-VRRP-是如何通信的？" class="headerlink" title="5.3 VRRP 是如何通信的？"></a>5.3 VRRP 是如何通信的？</h2><p>在一组虚拟路由器中，只有作为 MASTER 的 VRRP 路由器会一直发送 VRRP 广播包，此时 BACKUP 不会抢占 MASTER 。当 MASTER 不可用时，这个时候 BACKUP  就收不到来自 MASTER 的广播包了，此时多台 BACKUP 中优先级最高的路由器会去抢占为 MASTER。这种抢占是非常快速的（可能只有1秒甚至更少），以保证服务的连续性。出于安全性考虑，VRRP 数据包使用了加密协议进行了加密。</p>
<h1 id="6-Keepalived-高可用服务脑裂问题"><a href="#6-Keepalived-高可用服务脑裂问题" class="headerlink" title="6 Keepalived 高可用服务脑裂问题"></a>6 Keepalived 高可用服务脑裂问题</h1><h2 id="6-1-什么是脑裂？"><a href="#6-1-什么是脑裂？" class="headerlink" title="6.1 什么是脑裂？"></a>6.1 什么是脑裂？</h2><p>由于某些原因，导致两台高可用服务器在指定时间内，无法检测到对方的心跳消息，各自取得资源及服务的所有权，而此时的两台高可用服务器都还活着并在正常运行，这样就会导致同一个 IP 或服务在两端同时存在发生冲突，最严重的是两台主机占用同一个 VIP 地址，当用户写入数据时可能会分别写入到两端，这可能会导致服务器两端的数据不一致或造成数据丢失，这种情况就被称为脑裂。</p>
<h2 id="6-2-导致脑裂发生的原因"><a href="#6-2-导致脑裂发生的原因" class="headerlink" title="6.2 导致脑裂发生的原因"></a>6.2 导致脑裂发生的原因</h2><p>一般来说，脑裂的发生，有以下几种原因：</p>
<p>1）高可用服务器之间心跳线链路故障，导致无法正常通信。</p>
<p>心跳线坏了（包括断了，老化）<br>网卡及相关驱动坏了，IP 配置及冲突问题（网卡直连）<br>心跳线连接的设备故障（网卡及交换机）<br>2）高可用服务器上开启了 iptables 防火墙阻挡了心跳消息传输。</p>
<p>3）高可用服务器上心跳网卡地址等信息配置不正确，导致发送心跳失败。</p>
<p>4）其他服务配置不当等原因，如心跳方式不同，心跳广播冲突、软件 BUG等。</p>
<p>注意：Keepalived 配置里同一 VRRP 实例如果 virtual_router_id 参数两端配置不一致，也会导致脑裂问题发生。</p>
<h2 id="6-3-解决脑裂的具体方案"><a href="#6-3-解决脑裂的具体方案" class="headerlink" title="6.3 解决脑裂的具体方案"></a>6.3 解决脑裂的具体方案</h2><p>在实际生产环境中，可以从以下几个方面来防止脑裂问题的发生</p>
<p>1）同时使用串行电缆和以太网电缆连接，同时用两条心跳线路，这样一条线路坏了，另一个还是好的，依然能够传送心跳消息</p>
<p>2）当检测到脑裂时强行关闭一个心跳节点（这个功能需要特殊设备支持，如Stonith、fence）。相当于备节点接收不到心跳消息，发送关机命令通过单独的线路关闭主节点的电源。</p>
<p>3）做好对脑裂的监控报警（如邮件及手机短信等或值班），在问题发生时人为第一时间介入仲裁，降低损失。例如，百度的监控报警短信就有上行和下行的区别。报警信息报到管理员手机上，管理员可以通过手机回复对应数字或简单的字符串操作返回给服务器，让服务器根据指令自动处理相应故障，这样解决故障的时间更短。</p>
<p>4）如果开启防火墙，一定要让心跳消息通过，一般通过允许 IP 段的形式。</p>
<h1 id="7-KeepAlived-配置详解"><a href="#7-KeepAlived-配置详解" class="headerlink" title="7 KeepAlived 配置详解"></a>7 KeepAlived 配置详解</h1><p>Keepalived的所有配置都在一个配置文件里面，主要分为三类：</p>
<ul>
<li>全局配置</li>
<li>VRRPD配置</li>
<li>LVS 配置</li>
</ul>
<h2 id="7-1-全局配置"><a href="#7-1-全局配置" class="headerlink" title="7.1 全局配置"></a>7.1 全局配置</h2><p>全局配置是对整个 Keepalived 生效的配置，一个典型的配置如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">global_defs &#123;</span><br><span class="line">   notification_email &#123;         #设置 keepalived 在发生事件（比如切换）的时候，需要发送到的email地址，可以设置多个，每行一个。</span><br><span class="line">     acassen@firewall.loc</span><br><span class="line">     failover@firewall.loc</span><br><span class="line">     sysadmin@firewall.loc</span><br><span class="line">   &#125;</span><br><span class="line">   notification_email_from Alexandre.Cassen@firewall.loc    #设置通知邮件发送来自于哪里，如果本地开启了sendmail的话，可以使用上面的默认值。</span><br><span class="line">   smtp_server 192.168.200.1    #指定发送邮件的smtp服务器。</span><br><span class="line">   smtp_connect_timeout 30      #设置smtp连接超时时间，单位为秒。</span><br><span class="line">   router_id LVS_DEVEL          #是运行keepalived的一个表示，多个集群设置不同。</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="7-2-VRRPD配置"><a href="#7-2-VRRPD配置" class="headerlink" title="7.2 VRRPD配置"></a>7.2 VRRPD配置</h2><p>VRRPD 的配置是 Keepalived 比较重要的配置，主要分为两个部分 VRRP 同步组和 VRRP实例，也就是想要使用 VRRP 进行高可用选举，那么就一定需要配置一个VRRP实例，在实例中来定义 VIP、服务器角色等。</p>
<h3 id="7-2-1-VRRP-Sync-Groups"><a href="#7-2-1-VRRP-Sync-Groups" class="headerlink" title="7.2.1 VRRP Sync Groups**"></a>7.2.1 VRRP Sync Groups**</h3><p>不使用Sync Group的话，如果机器（或者说router）有两个网段，一个内网一个外网，每个网段开启一个VRRP实例，假设VRRP配置为检查内网，那么当外网出现问题时，VRRPD认为自己仍然健康，那么不会发生Master和Backup的切换，从而导致了问题。Sync group就是为了解决这个问题，可以把两个实例都放进一个Sync Group，这样的话，group里面任何一个实例出现问题都会发生切换。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">vrrp_sync_group VG_1&#123; #监控多个网段的实例</span><br><span class="line">group &#123;</span><br><span class="line">　　　　VI_1 #实例名</span><br><span class="line">　　　　VI_2</span><br><span class="line">　　　　......</span><br><span class="line">&#125;</span><br><span class="line">notify_master &#x2F;path&#x2F;xx.sh 　　　　#指定当切换到master时，执行的脚本</span><br><span class="line">netify_backup &#x2F;path&#x2F;xx.sh 　　　　#指定当切换到backup时，执行的脚本</span><br><span class="line">notify_fault &quot;path&#x2F;xx.sh VG_1&quot;   #故障时执行的脚本</span><br><span class="line">notify &#x2F;path&#x2F;xx.sh</span><br><span class="line">smtp_alert 　　#使用global_defs中提供的邮件地址和smtp服务器发送邮件通知</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7-2-2-VRRP实例（instance）配置"><a href="#7-2-2-VRRP实例（instance）配置" class="headerlink" title="7.2.2 VRRP实例（instance）配置"></a>7.2.2 VRRP实例（instance）配置</h3><p>VRRP实例就表示在上面开启了VRRP协议，这个实例说明了VRRP的一些特征，比如主从，VRID等，可以在每个interface上开启一个实例。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    state MASTER         #指定实例初始状态，实际的MASTER和BACKUP是选举决定的。</span><br><span class="line">    interface eth0       #指定实例绑定的网卡</span><br><span class="line">    virtual_router_id 51 #设置VRID标记，多个集群不能重复(0..255)</span><br><span class="line">    priority 100         #设置优先级，优先级高的会被竞选为Master，Master要高于BACKUP至少50</span><br><span class="line">    advert_int 1         #检查的时间间隔，默认1s</span><br><span class="line">    nopreempt            #设置为不抢占，说明：这个配置只能在BACKUP主机上面设置</span><br><span class="line">    preempt_delay        #抢占延迟，默认5分钟</span><br><span class="line">    debug                #debug级别</span><br><span class="line">    authentication &#123;     #设置认证</span><br><span class="line">        auth_type PASS    #认证方式，支持PASS和AH，官方建议使用PASS</span><br><span class="line">        auth_pass 1111    #认证的密码</span><br><span class="line">    &#125;</span><br><span class="line">    virtual_ipaddress &#123;     #设置VIP，可以设置多个，用于切换时的地址绑定。格式：#&lt;IPADDR&gt;&#x2F;&lt;MASK&gt; brd &lt;IPADDR&gt; dev &lt;STRING&gt; scope &lt;SCOPT&gt; label &lt;LABE</span><br><span class="line">        192.168.200.16&#x2F;24 dev eth0 label eth0:1</span><br><span class="line">        192.168.200.17&#x2F;24 dev eth1 label eth1:1</span><br><span class="line">        192.168.200.18</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="7-2-3-VRRP-脚本"><a href="#7-2-3-VRRP-脚本" class="headerlink" title="7.2.3 VRRP 脚本"></a>7.2.3 VRRP 脚本</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"># VRRP 脚本 </span><br><span class="line"># 如下所示为相关配置示例</span><br><span class="line">vrrp_script check_running &#123;</span><br><span class="line">   script &quot;&#x2F;usr&#x2F;local&#x2F;bin&#x2F;check_running&quot;</span><br><span class="line">   interval 10</span><br><span class="line">   weight 10</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_instance http &#123;</span><br><span class="line">   state BACKUP</span><br><span class="line">   smtp_alert</span><br><span class="line">   interface eth0</span><br><span class="line">   virtual_router_id 101</span><br><span class="line">   priority 90</span><br><span class="line">   advert_int 3</span><br><span class="line">   authentication &#123;</span><br><span class="line">   auth_type PASS</span><br><span class="line">   auth_pass whatever</span><br><span class="line">   &#125;</span><br><span class="line">   virtual_ipaddress &#123;</span><br><span class="line">   1.1.1.1</span><br><span class="line">   &#125;</span><br><span class="line">   track_script &#123;</span><br><span class="line">   check_running </span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br><span class="line"># 首先在 vrrp_script 区域定义脚本名字和脚本执行的间隔和脚本执行的优先级变更, 如下所示:</span><br><span class="line">vrrp_script check_running &#123;</span><br><span class="line">            script &quot;&#x2F;usr&#x2F;local&#x2F;bin&#x2F;check_running&quot;</span><br><span class="line">            interval 10     # 脚本执行间隔</span><br><span class="line">            weight 10       # 脚本结果导致的优先级变更 10 表示优先级 + 10-10 则表示优先级 - 10</span><br><span class="line">            &#125;</span><br><span class="line"># 然后在实例(vrrp_instance) 里面引用有点类似脚本里面的函数引用一样先定义后引用函数名</span><br><span class="line">track_script &#123;</span><br><span class="line">      check_running </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意:<br>VRRP 脚本 (vrrp_script) 和 VRRP 实例 (vrrp_instance) 属于同一个级别<br>keepalived 会定时执行脚本并对脚本执行的结果进行分析，动态调整 vrrp_instance 的优先级。一般脚本检测返回的值为 0，说明脚本检测成功，如果为非 0 数值，则说明检测失败<br>如果脚本执行结果为 0，并且 weight 配置的值大于 0，则优先级相应的增加, 如果 weight 为非 0，则优先级不变<br>如果脚本执行结果非 0，并且 weight 配置的值小于 0，则优先级相应的减少, 如果 weight 为 0，则优先级不变<br>其他情况，维持原本配置的优先级，即配置文件中 priority 对应的值。<br>这里需要注意的是：<br>1） 优先级不会不断的提高或者降低<br>2） 可以编写多个检测脚本并为每个检测脚本设置不同的 weight<br>3） 不管提高优先级还是降低优先级，最终优先级的范围是在[1,254]，不会出现优先级小于等于 0 或者优先级大于等于 255 的情况<br>这样可以做到利用脚本检测业务进程的状态，并动态调整优先级从而实现主备切换。</p>
<h2 id="7-3-LVS-配置"><a href="#7-3-LVS-配置" class="headerlink" title="7.3 LVS 配置"></a>7.3 LVS 配置</h2><p>虚拟服务器virtual_server定义块 ，虚拟服务器定义是keepalived框架最重要的项目了，是keepalived.conf必不可少的部分。 该部分是用来管理LVS的，是实现keepalive和LVS相结合的模块。ipvsadm命令可以实现的管理在这里都可以通过参数配置实现，注意：real_server是被包含在viyual_server模块中的，是子模块。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">virtual_server 192.168.202.200 23 &#123;        &#x2F;&#x2F;VIP地址，要和vrrp_instance模块中的virtual_ipaddress地址一致</span><br><span class="line">　　　　delay_loop 6   #健康检查时间间隔</span><br><span class="line">　　　　lb_algo rr 　　#lvs调度算法rr|wrr|lc|wlc|lblc|sh|dh</span><br><span class="line">　　　　lb_kind DR    #负载均衡转发规则NAT|DR|RUN</span><br><span class="line">　　　　persistence_timeout 5 #会话保持时间</span><br><span class="line">　　　　protocol TCP    #使用的协议</span><br><span class="line">　　　　persistence_granularity &lt;NETMASK&gt; #lvs会话保持粒度</span><br><span class="line">　　　　virtualhost &lt;string&gt;    #检查的web服务器的虚拟主机（host：头）</span><br><span class="line">　　　　sorry_server&lt;IPADDR&gt; &lt;port&gt; #备用机，所有realserver失效后启用</span><br><span class="line"></span><br><span class="line">real_server 192.168.200.5 23 &#123;             &#x2F;&#x2F;RS的真实IP地址</span><br><span class="line">            weight 1 #默认为1,0为失效</span><br><span class="line">            inhibit_on_failure #在服务器健康检查失效时，将其设为0，而不是直接从ipvs中删除</span><br><span class="line">            notify_up &lt;string&gt; | &lt;quoted-string&gt; #在检测到server up后执行脚本</span><br><span class="line">            notify_down &lt;string&gt; | &lt;quoted-string&gt; #在检测到server down后执行脚本</span><br><span class="line"></span><br><span class="line">TCP_CHECK &#123;                    &#x2F;&#x2F;常用</span><br><span class="line">            connect_timeout 3 #连接超时时间</span><br><span class="line">            nb_get_retry 3 #重连次数</span><br><span class="line">            delay_before_retry 3 #重连间隔时间</span><br><span class="line">            connect_port 23  #健康检查的端口的端口</span><br><span class="line">            bindto &lt;ip&gt;</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">HTTP_GET | SSL_GET&#123;          &#x2F;&#x2F;不常用</span><br><span class="line">    url&#123; #检查url，可以指定多个</span><br><span class="line">         path &#x2F;</span><br><span class="line">         digest &lt;string&gt; #检查后的摘要信息</span><br><span class="line">         status_code 200 #检查的返回状态码</span><br><span class="line">        &#125;</span><br><span class="line">    connect_port &lt;port&gt;</span><br><span class="line">    bindto &lt;IPADD&gt;</span><br><span class="line">    connect_timeout 5</span><br><span class="line">    nb_get_retry 3</span><br><span class="line">    delay_before_retry 2</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">SMTP_CHECK&#123;                 &#x2F;&#x2F;不常用</span><br><span class="line">    host&#123;</span><br><span class="line">    connect_ip &lt;IP ADDRESS&gt;</span><br><span class="line">    connect_port &lt;port&gt; #默认检查25端口</span><br><span class="line">    bindto &lt;IP ADDRESS&gt;</span><br><span class="line">         &#125;</span><br><span class="line">    connect_timeout 5</span><br><span class="line">    retry 3</span><br><span class="line">    delay_before_retry 2</span><br><span class="line">    helo_name &lt;string&gt; | &lt;quoted-string&gt; #smtp helo请求命令参数，可选</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">MISC_CHECK&#123;                 &#x2F;&#x2F;不常用</span><br><span class="line">    misc_path &lt;string&gt; | &lt;quoted-string&gt; #外部脚本路径</span><br><span class="line">    misc_timeout #脚本执行超时时间</span><br><span class="line">    misc_dynamic #如设置该项，则退出状态码会用来动态调整服务器的权重，返回0 正常，不修改；返回1，</span><br><span class="line"></span><br><span class="line">　　检查失败，权重改为0；返回2-255，正常，权重设置为：返回状态码-2</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/07/06/docs/JVM/%E7%BC%96%E8%AF%91JDK/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/07/06/docs/JVM/%E7%BC%96%E8%AF%91JDK/" class="post-title-link" itemprop="url">编译JDK</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-07-06 11:06:55" itemprop="dateCreated datePublished" datetime="2021-07-06T11:06:55+08:00">2021-07-06</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:46:35" itemprop="dateModified" datetime="2022-05-10T09:46:35+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java/" itemprop="url" rel="index"><span itemprop="name">Java</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-macOS-编译-OpenJDK"><a href="#1-macOS-编译-OpenJDK" class="headerlink" title="1 macOS 编译 OpenJDK"></a>1 macOS 编译 OpenJDK</h1><p>目标：编译 OpenJDK17</p>
<h2 id="1-1-准备编译环境"><a href="#1-1-准备编译环境" class="headerlink" title="1.1 准备编译环境"></a>1.1 准备编译环境</h2><ol>
<li>首先去应用商店安装 <code>xcode.app</code></li>
<li>安装 <code>JDK16</code>（比要编译的JDK低一个版本，如要编译的openjdk17,那就安装jdk16）</li>
<li><code>brew install freetype ccache</code></li>
</ol>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/07/06/docs/JVM/%E7%BC%96%E8%AF%91JDK/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/20/MySQL%E4%B8%BB%E4%BB%8E%E6%90%AD%E5%BB%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/20/MySQL%E4%B8%BB%E4%BB%8E%E6%90%AD%E5%BB%BA/" class="post-title-link" itemprop="url">MySQL主从搭建</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-06-20 16:45:19 / 修改时间：17:30:29" itemprop="dateCreated datePublished" datetime="2021-06-20T16:45:19+08:00">2021-06-20</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mysql/" itemprop="url" rel="index"><span itemprop="name">mysql</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="关键点"><a href="#关键点" class="headerlink" title="关键点"></a>关键点</h1><ul>
<li><p>主配置 log-bin，指定文件的名字</p>
</li>
<li><p>主配置 server-id，默认为1</p>
</li>
<li><p>从 server-id 与主不能重复</p>
</li>
<li><p>主数据库创建备份账户并授权 <code>REPLICATION SLAVE</code></p>
</li>
<li><p>主数据库锁表 <code>FLUSH TABLES WITH READ LOCK</code></p>
</li>
<li><p>主数据库找到 <code>log-bin</code> 的位置 <code>SHOW MASTER STATUS</code></p>
</li>
<li><p>备份主数据库数据 <code>mysqldump -all-datables --master-data &gt; dbduump.db</code></p>
</li>
<li><p>主数据库解锁 <code>unlock tables</code></p>
</li>
<li><p>从数据库导入 dump的数据</p>
</li>
<li><p>在从数据库上设置主数据库的配置</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mysql<span class="operator">&gt;</span> CHANGE MASTER <span class="keyword">TO</span></span><br><span class="line">	<span class="operator">-</span><span class="operator">&gt;</span> MASTER_HOST<span class="operator">=</span><span class="string">&#x27;master_host_name&#x27;</span>, 	</span><br><span class="line">	<span class="operator">-</span><span class="operator">&gt;</span> MASTER_PORT<span class="operator">=</span>port_num </span><br><span class="line">	<span class="operator">-</span><span class="operator">&gt;</span> MASTER_USER<span class="operator">=</span><span class="string">&#x27;replication_user_name&#x27;</span>, </span><br><span class="line">	<span class="operator">-</span><span class="operator">&gt;</span> MASTER_PASSWORD<span class="operator">=</span><span class="string">&#x27;replication_password&#x27;</span>, 			        </span><br><span class="line">	<span class="operator">-</span><span class="operator">&gt;</span> MASTER_LOG_FILE<span class="operator">=</span><span class="string">&#x27;recorded_log_file_name&#x27;</span>,			   </span><br><span class="line">  <span class="operator">-</span><span class="operator">&gt;</span> MASTER_LOG_POS<span class="operator">=</span>recorded_log_position;                                                       </span><br></pre></td></tr></table></figure>

<ul>
<li>master_host_name : MySQL主的地址</li>
<li>port_num : MySQL主的端口（数字型）</li>
<li>replication_user_name : 备份账户的用户名</li>
<li>replication_password : 备份账户的密码</li>
<li>recorded_log_file_name ：bin-log的文件名</li>
<li>recorded_log_position : bin-log的位置（数字型）</li>
<li>bin-log的文件名和位置 是 从 <code>show master status</code> 得到的。</li>
</ul>
</li>
</ul>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/06/20/MySQL%E4%B8%BB%E4%BB%8E%E6%90%AD%E5%BB%BA/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/09/docs/MySQL/%E6%90%AD%E5%BB%BAMycat/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/09/docs/MySQL/%E6%90%AD%E5%BB%BAMycat/" class="post-title-link" itemprop="url">搭建Mycat</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-06-09 20:51:05" itemprop="dateCreated datePublished" datetime="2021-06-09T20:51:05+08:00">2021-06-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-10 09:47:01" itemprop="dateModified" datetime="2022-05-10T09:47:01+08:00">2022-05-10</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" itemprop="url" rel="index"><span itemprop="name">数据库</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-环境搭建"><a href="#1-环境搭建" class="headerlink" title="1 环境搭建"></a>1 环境搭建</h1><ol>
<li><p>3台服务器</p>
</li>
<li><p>centos 7</p>
</li>
<li><p>采用 yum 方式，在其中两台安装 mysql</p>
</li>
<li><p>检查mysql 安装是否正确</p>
</li>
<li><p>下载 Mycat 软件包</p>
</li>
<li><p>在第3台机器上安装mycat，并修改配置文件</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210609210635587.png" alt="image-20210609210635587"></p>
</li>
<li><p>连接mycat，体验数据的增删改查</p>
</li>
</ol>
<h1 id="2-mysql-安装教程"><a href="#2-mysql-安装教程" class="headerlink" title="2 mysql 安装教程"></a>2 mysql 安装教程</h1><h2 id="2-1-查询是否安装了mysql"><a href="#2-1-查询是否安装了mysql" class="headerlink" title="2.1 查询是否安装了mysql**"></a>2.1 查询是否安装了mysql**</h2>  <figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rpm -qa|grep mysql                                                      </span><br></pre></td></tr></table></figure>

<h2 id="2-2-卸载mysql-（下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）"><a href="#2-2-卸载mysql-（下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）" class="headerlink" title="2.2 卸载mysql （下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）**"></a>2.2 卸载mysql （下面是卸载mysql的库，防止产生冲突，mysql也是类似卸载方式）**</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">rpm -e --nodeps mysql-libs-5.1.*</span><br><span class="line">卸载之后，记得：</span><br><span class="line">find &#x2F; -name mysql</span><br><span class="line">删除查询出来的所有东西</span><br></pre></td></tr></table></figure>

<h2 id="2-3-安装mysql"><a href="#2-3-安装mysql" class="headerlink" title="2.3 安装mysql"></a>2.3 安装mysql</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install mysql-server                                                    </span><br></pre></td></tr></table></figure>

<p>注意: centos 7这样安装不行, 详见文档底部</p>
<h2 id="2-4-启动mysql"><a href="#2-4-启动mysql" class="headerlink" title="2.4 启动mysql"></a>2.4 启动mysql</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">启动方式1：service mysql start</span><br><span class="line">启动方式2：&#x2F;etc&#x2F;init.d&#x2F;mysql start</span><br><span class="line">启动方式3：service mysqld start</span><br><span class="line">启动方式4：&#x2F;etc&#x2F;init.d&#x2F;mysqld start                                               </span><br></pre></td></tr></table></figure>

<h2 id="2-5-root账户默认是没有密码的，修改root密码："><a href="#2-5-root账户默认是没有密码的，修改root密码：" class="headerlink" title="2.5 root账户默认是没有密码的，修改root密码："></a>2.5 root账户默认是没有密码的，修改root密码：</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;usr&#x2F;bin&#x2F;mysqladmin -u root password 密码 </span><br><span class="line">例如：</span><br><span class="line">&#x2F;usr&#x2F;bin&#x2F;mysqladmin -u root password pwd    这样就将root密码设置成pwd了       </span><br></pre></td></tr></table></figure>

<h2 id="2-6-重置root密码（忘记root密码找回）"><a href="#2-6-重置root密码（忘记root密码找回）" class="headerlink" title="2.6 重置root密码（忘记root密码找回）"></a>2.6 重置root密码（忘记root密码找回）</h2><h3 id="2-6-1-停止MySQL服务命令"><a href="#2-6-1-停止MySQL服务命令" class="headerlink" title="2.6.1 停止MySQL服务命令:"></a>2.6.1 停止MySQL服务命令:</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;init.d&#x2F;mysqld stop </span><br><span class="line">&#x2F;etc&#x2F;init.d&#x2F;mysql stop                                                        </span><br></pre></td></tr></table></figure>

<h3 id="2-6-2-输入绕过密码认证命令："><a href="#2-6-2-输入绕过密码认证命令：" class="headerlink" title="2.6.2 输入绕过密码认证命令："></a>2.6.2 输入绕过密码认证命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysqld_safe --user&#x3D;mysql --skip-grant-tables --skip-networking &amp;</span><br></pre></td></tr></table></figure>

<h3 id="1-6-3-输入登录用户命令："><a href="#1-6-3-输入登录用户命令：" class="headerlink" title="1.6.3 输入登录用户命令："></a>1.6.3 输入登录用户命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -u root mysql                                                        </span><br></pre></td></tr></table></figure>

<h3 id="2-6-4-输入修改root密码SQL语句："><a href="#2-6-4-输入修改root密码SQL语句：" class="headerlink" title="2.6.4 输入修改root密码SQL语句："></a>2.6.4 输入修改root密码SQL语句：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">update user set Password&#x3D;password (&#39;123456&#39;) where user&#x3D;&#39;root&#39;;          </span><br></pre></td></tr></table></figure>

<h3 id="2-6-5-输入数据刷新命令："><a href="#2-6-5-输入数据刷新命令：" class="headerlink" title="2.6.5 输入数据刷新命令："></a>2.6.5 输入数据刷新命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">FLUSH PRIVILEGES;                                                   </span><br></pre></td></tr></table></figure>

<h3 id="2-6-6-退出MySQL命令："><a href="#2-6-6-退出MySQL命令：" class="headerlink" title="2.6.6 退出MySQL命令："></a>2.6.6 退出MySQL命令：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">quit;                                                              </span><br></pre></td></tr></table></figure>

<h2 id="2-7-设置允许远程连接"><a href="#2-7-设置允许远程连接" class="headerlink" title="2.7 设置允许远程连接"></a>2.7 设置允许远程连接</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">grant all privileges on *.* to root@&#39;%&#39; identified by &#39;123456789&#39; with grant option;  </span><br></pre></td></tr></table></figure>

<h2 id="2-8-开放端口3306，否则依然无法过远程"><a href="#2-8-开放端口3306，否则依然无法过远程" class="headerlink" title="2.8 开放端口3306，否则依然无法过远程"></a>2.8 开放端口3306，否则依然无法过远程</h2><h3 id="2-8-1-打开防火墙配置文件："><a href="#2-8-1-打开防火墙配置文件：" class="headerlink" title="2.8.1 打开防火墙配置文件："></a>2.8.1 打开防火墙配置文件：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi &#x2F;etc&#x2F;sysconfig&#x2F;iptables                                                   </span><br></pre></td></tr></table></figure>

<h3 id="2-8-2-添加下面一行："><a href="#2-8-2-添加下面一行：" class="headerlink" title="2.8.2 添加下面一行："></a>2.8.2 添加下面一行：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT</span><br></pre></td></tr></table></figure>

<p>注意：开通3306 端口的行必须在icmp-host-prohibited前，否则无效：以下为配置结果图：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/5e81a3ba087c42ce05540189.jpg" alt="图片描述"></p>
<h3 id="2-8-3-重启防火墙，使配置生效："><a href="#2-8-3-重启防火墙，使配置生效：" class="headerlink" title="2.8.3 重启防火墙，使配置生效："></a>2.8.3 重启防火墙，使配置生效：</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;etc&#x2F;init.d&#x2F;iptables restart                                                </span><br></pre></td></tr></table></figure>

<h2 id="2-9-设置开机启动mysql："><a href="#2-9-设置开机启动mysql：" class="headerlink" title="2.9 设置开机启动mysql："></a>2.9 设置开机启动mysql：</h2><h3 id="2-9-1-查看MySQL服务是否自动开启命令"><a href="#2-9-1-查看MySQL服务是否自动开启命令" class="headerlink" title="2.9.1 查看MySQL服务是否自动开启命令"></a>2.9.1 查看MySQL服务是否自动开启命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">chkconfig --list | grep mysqld</span><br><span class="line">chkconfig --list | grep mysql                                            </span><br></pre></td></tr></table></figure>

<h3 id="2-9-2-开启MySQL服务自动开启命令"><a href="#2-9-2-开启MySQL服务自动开启命令" class="headerlink" title="2.9.2 开启MySQL服务自动开启命令"></a>2.9.2 开启MySQL服务自动开启命令</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">chkconfig mysqld on</span><br><span class="line">chkconfig mysql on                                              </span><br></pre></td></tr></table></figure>

<h2 id="2-10-将mysql默认引擎设置为InnoDB"><a href="#2-10-将mysql默认引擎设置为InnoDB" class="headerlink" title="2.10 将mysql默认引擎设置为InnoDB"></a>2.10 将mysql默认引擎设置为InnoDB</h2><p>修改MySQL配置文件my.cnf</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;etc</span><br><span class="line">vi my.cnf                                                         </span><br></pre></td></tr></table></figure>

<p>在[mysqld]一段加入</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">default-storage-engine&#x3D;InnoDB                                            </span><br></pre></td></tr></table></figure>

<p>删除ib_logfile0、ib_logfile1两个文件</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd &#x2F;var&#x2F;lib&#x2F;mysql</span><br><span class="line">rm -rf ib_logfile*                                                   </span><br></pre></td></tr></table></figure>

<p>重启mysql</p>
<h2 id="2-11-开启mysql的日志-监控执行的sql语句"><a href="#2-11-开启mysql的日志-监控执行的sql语句" class="headerlink" title="2.11 开启mysql的日志(监控执行的sql语句)"></a>2.11 开启mysql的日志(监控执行的sql语句)</h2><p>命令: <code>show global variables like ‘%general%’;</code> 该语句可以查看是否开启, 以及生成的位置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">set global general_log &#x3D; on; &#x2F;&#x2F; 打开  </span><br><span class="line">set global general_log &#x3D; off; &#x2F;&#x2F; 关闭      </span><br></pre></td></tr></table></figure>

<p>参考文档:</p>
<p><a target="_blank" rel="noopener" href="http://blog.csdn.net/fdipzone/article/details/16995303">http://blog.csdn.net/fdipzone/article/details/16995303</a></p>
<h2 id="2-12-centos7安装mysql"><a href="#2-12-centos7安装mysql" class="headerlink" title="2.12 centos7安装mysql"></a>2.12 centos7安装mysql</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">wget http:&#x2F;&#x2F;dev.mysql.com&#x2F;get&#x2F;mysql-community-release-el7-5.noarch.rpm</span><br><span class="line">#rpm -ivh mysql-community-release-el7-5.noarch.rpm</span><br><span class="line">#yum install mysql-community-server</span><br><span class="line">成功安装之后重启mysql服务</span><br><span class="line">#service mysqld restart</span><br><span class="line">初次安装mysql是root账户是没有密码的</span><br><span class="line">设置密码的方法</span><br><span class="line">#mysql -uroot</span><br><span class="line">mysql&gt; set password for ‘root’@‘localhost’ &#x3D; password(&#39;mypasswd&#39;);</span><br><span class="line">mysql&gt; exit</span><br></pre></td></tr></table></figure>

<h1 id="3-Mycat-安装教程"><a href="#3-Mycat-安装教程" class="headerlink" title="3 Mycat 安装教程"></a>3 Mycat 安装教程</h1><p><a target="_blank" rel="noopener" href="http://www.mycat.org.cn/">mycat官网</a></p>
<p><a target="_blank" rel="noopener" href="https://www.yuque.com/ccazhw/tuacvk">mycat1权威指南</a></p>
<p><a target="_blank" rel="noopener" href="https://www.yuque.com/ccazhw/ml3nkf">mycat2权威指南</a></p>
<h2 id="3-1-安装-Mycat"><a href="#3-1-安装-Mycat" class="headerlink" title="3.1 安装 Mycat"></a>3.1 安装 Mycat</h2><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf Mycat-server-1.6.7.1-release-20200209222254-mac.tar</span><br></pre></td></tr></table></figure>

<h2 id="3-2-启动和验证"><a href="#3-2-启动和验证" class="headerlink" title="3.2 启动和验证"></a>3.2 启动和验证</h2><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">bin/mycat start</span><br></pre></td></tr></table></figure>

<p>启动后，验证一些基本操作，如下图所示：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210610162658349.png" alt="image-20210610162658349"></p>
<p>可以看到，我们成功连上了 mycat 服务器，MyCat 服务器默认定义了一个名为 TESTDB 的逻辑数据库，并且也在该逻辑数据库中定义了一些逻辑表。</p>
<p>但当我们尝试做一些 select 操作的时候，控制台会提示报错，这是因为 MyCat 配置错误导致的。</p>
<p>所以我们需要进行配置。</p>
<h2 id="3-3-配置"><a href="#3-3-配置" class="headerlink" title="3.3 配置"></a>3.3 配置</h2><p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210610163531656.png" alt="image-20210610163531656"></p>
<p>其中 ,</p>
<ul>
<li>bin 目录是 MyCat 的启动目录，</li>
<li>conf 目录是 MyCat 的配置文件目录，</li>
<li>lib 目录是 MyCat 自身的 Jar 包以及所依赖 Jar 包的目录，</li>
<li>logs 目录是日志目录。</li>
</ul>
<p>在 conf 目录下有 3 个重要的配置文件：</p>
<ul>
<li>schema.xml</li>
<li>Server.xml</li>
<li>Rule.xml</li>
</ul>
<p>下面就来简单说明这 3 个配置文件的关键配置</p>
<h3 id="3-3-1-schema-xml"><a href="#3-3-1-schema-xml" class="headerlink" title="3.3.1 schema.xml"></a>3.3.1 schema.xml</h3><p>schema.xml 文件定义了 MyCat 到底连接那个数据库实例，连接这个数据库实例的哪个数据库。MyCat 一共有几个逻辑数据库，MyCat 一共有几个逻辑表。</p>
<p>schema.xml 文件一共有四个配置节点：<code>DataHost</code>、<code>DataNode</code>、<code>Schema</code>、<code>Table</code>。</p>
<ul>
<li><p>DataHost：定义数据库实例</p>
<ul>
<li><p>balance：负载均衡类型</p>
<ul>
<li>balance=”0”, 不开启读写分离机制，所有读操作都发送到当前可用的writeHost上。</li>
<li>balance=”1”，全部的readHost与stand by writeHost参与select语句的负载均衡，简单的说，当双主双从模式(M1-&gt;S1，M2-&gt;S2，并且M1与 M2互为主备)，正常情况下，M2,S1,S2都参与select语句的负载均衡。</li>
<li>balance=”2”，所有读操作都随机的在writeHost、readhost上分发。</li>
<li>balance=”3”，所有读请求随机的分发到wiriterHost对应的readhost执行，writerHost不负担读压力，注意balance=3只在1.4及其以后版本有，1.3没有。</li>
</ul>
</li>
<li><p>writeType：写请求类型，0落在第一个writeHost上；1随机；</p>
</li>
</ul>
</li>
<li><p>DataNode：定义数据库名称</p>
</li>
<li><p>Schema：定义逻辑库</p>
<ul>
<li><p>checkSQLschema：是否去掉SQL中的schema</p>
</li>
<li><p>sqlMaxLimit：select 默认的<code>limit</code>值，仅对分片表有效</p>
</li>
<li><p>rule：定义分片表的分片规则，必须与<code>rule.xml</code>中的<code>tableRule</code>对应</p>
</li>
<li><p>ruleRequired：是否绑定分片规则，如果为true，没有绑定分片规则，程序报错</p>
</li>
</ul>
</li>
<li><p>Table：定义逻辑表</p>
</li>
</ul>
<p>DataHost 节点定义了 MyCat 要连接哪个 MySQL 实例，连接的账号密码是多少。默认的 MyCat 为我们定义了一个名为 localhost1 的数据服务器（DataHost），它指向了本地（localhost）3306 端口的 MySQL 服务器，对应 MySQL 服务器的账号是 root，密码是 123456。</p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dataHost</span> <span class="attr">name</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">maxCon</span>=<span class="string">&quot;1000&quot;</span> <span class="attr">minCon</span>=<span class="string">&quot;10&quot;</span> <span class="attr">balance</span>=<span class="string">&quot;0&quot;</span> <span class="attr">writeType</span>=<span class="string">&quot;0&quot;</span> <span class="attr">dbType</span>=<span class="string">&quot;mysql&quot;</span> <span class="attr">dbDriver</span>=<span class="string">&quot;native&quot;</span> <span class="attr">switchType</span>=<span class="string">&quot;1&quot;</span>  <span class="attr">slaveThreshold</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">heartbeat</span>&gt;</span>select user()<span class="tag">&lt;/<span class="name">heartbeat</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;hostM1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;localhost:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span>&gt;</span> </span><br><span class="line">		<span class="tag">&lt;<span class="name">readHost</span> <span class="attr">host</span>=<span class="string">&quot;hostS2&quot;</span> <span class="attr">url</span>=<span class="string">&quot;192.168.1.200:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;xxx&quot;</span> /&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">writeHost</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;hostS1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;localhost:3316&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span> /&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">dataHost</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>DataNode 节点指定了需要连接的具体数据库名称，其使用一个 dataHost 属性指定该数据库位于哪个数据库实例上。默认的 MyCat 为我们创建了三个数据节点（DataNode），dn1 数据节点对应 localhost1 数据服务器上的 db1 数据库，dn2 数据节点对应 localhost1 数据服务器上的 db2 数据库，dn1 数据节点对应 localhost1 数据服务器上的 db3 数据库。</p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn1&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;db1&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn2&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;db2&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn3&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;db3&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>Schema 节点定义了 MyCat 的所有逻辑数据库，Table 节点定义了 MyCat 的所有逻辑表。默认的 MyCat 为我们定义了一个名为 TESTDB 的逻辑数据库，在这个逻辑数据库下又定义了名为 travaelrecord、company 等 6 个逻辑表。</p>
  <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">schema</span> <span class="attr">name</span>=<span class="string">&quot;TESTDB&quot;</span> <span class="attr">checkSQLschema</span>=<span class="string">&quot;false&quot;</span> <span class="attr">sqlMaxLimit</span>=<span class="string">&quot;100&quot;</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- auto sharding by id (long) --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;travelrecord&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2,dn3&quot;</span> <span class="attr">rule</span>=<span class="string">&quot;auto-sharding-long&quot;</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">		<span class="comment">&lt;!-- global table is auto cloned to all defined data nodes ,so can join</span></span><br><span class="line"><span class="comment">			with any table whose sharding node is in the same data node --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;company&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">type</span>=<span class="string">&quot;global&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2,dn3&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;goods&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">type</span>=<span class="string">&quot;global&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2&quot;</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- random sharding using mod sharind rule --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;hotnews&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">autoIncrement</span>=<span class="string">&quot;true&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2,dn3&quot;</span></span></span><br><span class="line"><span class="tag">			   <span class="attr">rule</span>=<span class="string">&quot;mod-long&quot;</span> /&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- &lt;table name=&quot;dual&quot; primaryKey=&quot;ID&quot; dataNode=&quot;dnx,dnoracle2&quot; type=&quot;global&quot;</span></span><br><span class="line"><span class="comment">			needAddLimit=&quot;false&quot;/&gt; &lt;table name=&quot;worker&quot; primaryKey=&quot;ID&quot; dataNode=&quot;jdbc_dn1,jdbc_dn2,jdbc_dn3&quot;</span></span><br><span class="line"><span class="comment">			rule=&quot;mod-long&quot; /&gt; --&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;employee&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2&quot;</span></span></span><br><span class="line"><span class="tag">			   <span class="attr">rule</span>=<span class="string">&quot;sharding-by-intfile&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;customer&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">dataNode</span>=<span class="string">&quot;dn1,dn2&quot;</span></span></span><br><span class="line"><span class="tag">			   <span class="attr">rule</span>=<span class="string">&quot;sharding-by-intfile&quot;</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">childTable</span> <span class="attr">name</span>=<span class="string">&quot;orders&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">joinKey</span>=<span class="string">&quot;customer_id&quot;</span></span></span><br><span class="line"><span class="tag">						<span class="attr">parentKey</span>=<span class="string">&quot;id&quot;</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">childTable</span> <span class="attr">name</span>=<span class="string">&quot;order_items&quot;</span> <span class="attr">joinKey</span>=<span class="string">&quot;order_id&quot;</span></span></span><br><span class="line"><span class="tag">							<span class="attr">parentKey</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">childTable</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">childTable</span> <span class="attr">name</span>=<span class="string">&quot;customer_addr&quot;</span> <span class="attr">primaryKey</span>=<span class="string">&quot;ID&quot;</span> <span class="attr">joinKey</span>=<span class="string">&quot;customer_id&quot;</span></span></span><br><span class="line"><span class="tag">						<span class="attr">parentKey</span>=<span class="string">&quot;id&quot;</span> /&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">		<span class="comment">&lt;!-- &lt;table name=&quot;oc_call&quot; primaryKey=&quot;ID&quot; dataNode=&quot;dn1$0-743&quot; rule=&quot;latest-month-calldate&quot;</span></span><br><span class="line"><span class="comment">			/&gt; --&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">schema</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>所以上面当我们登陆 MyCat 输入<code>show databases</code>会看到只有一个名为 TESTDB 的数据库，这个就是 MyCat 的逻辑数据库。</p>
<h3 id="3-3-2-server-xml"><a href="#3-3-2-server-xml" class="headerlink" title="3.3.2 server.xml"></a>3.3.2 server.xml</h3><p>server.xml 定义了项目中连接 MyCat 服务器所需要的账号密码，以及该账号能访问那些逻辑数据库。 server.xml 配置文件中有 <code>System</code> 和 <code>User</code> 两个配置节点。</p>
<p>System 节点定义了连接 MyCat 服务器的系统配置信息。例如是否开启实时统计功能，是否开启全加班一致性检测等。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">system</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;useSqlStat&quot;</span>&gt;</span>0<span class="tag">&lt;/<span class="name">property</span>&gt;</span>  <span class="comment">&lt;!-- 1为开启实时统计、0为关闭 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;useGlobleTableCheck&quot;</span>&gt;</span>0<span class="tag">&lt;/<span class="name">property</span>&gt;</span>  <span class="comment">&lt;!-- 1为开启全加班一致性检测、0为关闭 --&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;sequnceHandlerType&quot;</span>&gt;</span>2<span class="tag">&lt;/<span class="name">property</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;processorBufferPoolType&quot;</span>&gt;</span>0<span class="tag">&lt;/<span class="name">property</span>&gt;</span> </span><br><span class="line">	……</span><br><span class="line"><span class="tag">&lt;/<span class="name">system</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>User 配置节点定义了连接 MyCat 服务器的账号密码，以及该账号密码所能进行的数据库操作。默认的 MyCat 为我们创建了一个账户名为 root，密码为 123456 的账号，只能访问 TESTDB 逻辑数据库，并且定义了对相关表的操作权限。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">user</span> <span class="attr">name</span>=<span class="string">&quot;root&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;password&quot;</span>&gt;</span>123456<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;schemas&quot;</span>&gt;</span>TESTDB<span class="tag">&lt;/<span class="name">property</span>&gt;</span> </span><br><span class="line">	<span class="tag">&lt;<span class="name">privileges</span> <span class="attr">check</span>=<span class="string">&quot;false&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">schema</span> <span class="attr">name</span>=<span class="string">&quot;TESTDB&quot;</span> <span class="attr">dml</span>=<span class="string">&quot;0110&quot;</span> &gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;tb01&quot;</span> <span class="attr">dml</span>=<span class="string">&quot;0000&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">table</span> <span class="attr">name</span>=<span class="string">&quot;tb02&quot;</span> <span class="attr">dml</span>=<span class="string">&quot;1111&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">schema</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">privileges</span>&gt;</span>	 </span><br><span class="line"><span class="tag">&lt;/<span class="name">user</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-3-3-rule-xml"><a href="#3-3-3-rule-xml" class="headerlink" title="3.3.3 rule.xml"></a>3.3.3 rule.xml</h3><p>rule.xml 定义了逻辑表使用哪个字段进行拆分，使用什么拆分算法进行拆分。rule.xml 中有两个配置节点，分别是：<code>TableRule</code> 和 <code>Function</code> 配置节点。</p>
<p>TableRule 配置节点定义了逻辑表的拆分信息，例如使用哪个字段进行拆分，使用什么拆分算法。默认的 MyCat 为我们配置了一个名为 rule2 的表拆分规则，表示根据 user_id 字段进行拆分，拆分算法是 func1。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">tableRule</span> <span class="attr">name</span>=<span class="string">&quot;rule2&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">rule</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">columns</span>&gt;</span>user_id<span class="tag">&lt;/<span class="name">columns</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">algorithm</span>&gt;</span>func1<span class="tag">&lt;/<span class="name">algorithm</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">rule</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">tableRule</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>Function 配置节点则定义了具体的拆分算法。例如使用对 1000 取余的拆分算法，对 100 取余的拆分算分等等。默认的 MyCat 为我们定义了一个名为<code> func1</code> 的<strong>拆分算法</strong>，这个拆分算法定义在 <code>io.mycat.route.function.PartitionByLong</code> 类中，并且还传入了两个参数值。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">function</span> <span class="attr">name</span>=<span class="string">&quot;func1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;io.mycat.route.function.PartitionByLong&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;partitionCount&quot;</span>&gt;</span>8<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">&quot;partitionLength&quot;</span>&gt;</span>128<span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">function</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="3-4-Mycat-FAQ"><a href="#3-4-Mycat-FAQ" class="headerlink" title="3.4 Mycat FAQ"></a>3.4 Mycat FAQ</h2><h3 id="3-4-1-ERROR-1184-HY000-Invalid-DataSource-1"><a href="#3-4-1-ERROR-1184-HY000-Invalid-DataSource-1" class="headerlink" title="3.4.1 ERROR 1184 (HY000): Invalid DataSource:1"></a>3.4.1 ERROR 1184 (HY000): Invalid DataSource:1</h3><p>具体错误如下：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210611093234112.png" alt="image-20210611093234112"></p>
<p>错误原因有两种可能：</p>
<ol>
<li><p>没有为mysql用户配置远程访问的权限</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">writeHost</span> <span class="attr">host</span>=<span class="string">&quot;db1&quot;</span> <span class="attr">url</span>=<span class="string">&quot;192.168.0.3:3306&quot;</span> <span class="attr">user</span>=<span class="string">&quot;root&quot;</span> <span class="attr">password</span>=<span class="string">&quot;123456&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>

<p>授予mysql用户远程访问的权限</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">   <span class="keyword">GRANT</span> <span class="keyword">ALL</span> PRIVILEGES <span class="keyword">ON</span> <span class="operator">*</span>.<span class="operator">*</span> <span class="keyword">TO</span> <span class="string">&#x27;root&#x27;</span>@<span class="string">&#x27;%&#x27;</span> IDENTIFIED <span class="keyword">BY</span> <span class="string">&#x27;123456&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 没有在mysql中创建数据库</span><br><span class="line"></span><br><span class="line">   ```<span class="keyword">sql</span></span><br><span class="line">   # 此操作在当前机的mysql上操作（不再mycat）</span><br><span class="line">   # mysql <span class="operator">-</span>uroot <span class="operator">-</span>p</span><br><span class="line">   <span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> mycatdb1 <span class="keyword">DEFAULT</span> CHARSET utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_general_ci;</span><br><span class="line">   <span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> mycatdb2 <span class="keyword">DEFAULT</span> CHARSET utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_general_ci;</span><br><span class="line">   <span class="keyword">CREATE</span> DATABASE IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span> mycatdb3 <span class="keyword">DEFAULT</span> CHARSET utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_general_ci;</span><br><span class="line">   </span><br><span class="line">   # 三个分库各自创建表travelrecord</span><br><span class="line">   <span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `travelrecord` (</span><br><span class="line">     `id` <span class="type">int</span>(<span class="number">10</span>) unsigned <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line">     `name` <span class="type">varchar</span>(<span class="number">22</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">     `<span class="type">time</span>` <span class="type">int</span>(<span class="number">10</span>) unsigned <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="string">&#x27;0&#x27;</span>,</span><br><span class="line">     <span class="keyword">PRIMARY</span> KEY (`id`)</span><br><span class="line">   ) ENGINE<span class="operator">=</span>InnoDB AUTO_INCREMENT<span class="operator">=</span><span class="number">1</span> <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8mb4;</span><br><span class="line">   </span><br><span class="line">   # 模拟数据</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db1`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;qkl&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db1`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;andy&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db2`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;zgq&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br><span class="line">   <span class="keyword">INSERT</span> <span class="keyword">INTO</span> `mycat<span class="operator">-</span>db3`.`travelrecord` (`name`, `<span class="type">time</span>`) <span class="keyword">VALUES</span> (<span class="string">&#x27;pcb&#x27;</span>, <span class="string">&#x27;0&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>在 mycat 的配置文件 schema.xml 中修改 <code>dataNode</code>节点：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn1&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;mycatdb1&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn2&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;mycatdb2&quot;</span> /&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dataNode</span> <span class="attr">name</span>=<span class="string">&quot;dn3&quot;</span> <span class="attr">dataHost</span>=<span class="string">&quot;localhost1&quot;</span> <span class="attr">database</span>=<span class="string">&quot;mycatdb3&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure></li>
</ol>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/09/Netty%E7%9A%84%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/09/Netty%E7%9A%84%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6/" class="post-title-link" itemprop="url">Netty的核心组件</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-06-09 11:45:56" itemprop="dateCreated datePublished" datetime="2021-06-09T11:45:56+08:00">2021-06-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-09-01 08:51:26" itemprop="dateModified" datetime="2021-09-01T08:51:26+08:00">2021-09-01</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/netty/" itemprop="url" rel="index"><span itemprop="name">netty</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="1-Channel-接口"><a href="#1-Channel-接口" class="headerlink" title="1 Channel 接口"></a>1 Channel 接口</h1><p>基本的 I/O 操作(bind()、connect()、read()和 write())依赖于底层网络传输所提 供的原语。在基于 Java 的网络编程中，其基本的构造是 class Socket。Netty 的 Channel 接 口所提供的 API，大大地降低了直接使用 Socket 类的复杂性。此外，Channel 也是拥有许多 预定义的、专门化实现的广泛类层次结构的根，下面是一个简短的部分清单:</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/06/09/Netty%E7%9A%84%E6%A0%B8%E5%BF%83%E7%BB%84%E4%BB%B6/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/08/SpringBoot%E6%A0%A1%E9%AA%8C%E6%A1%86%E6%9E%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/08/SpringBoot%E6%A0%A1%E9%AA%8C%E6%A1%86%E6%9E%B6/" class="post-title-link" itemprop="url">SpringBoot校验框架</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-06-08 16:23:59 / 修改时间：16:29:39" itemprop="dateCreated datePublished" datetime="2021-06-08T16:23:59+08:00">2021-06-08</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/spring/" itemprop="url" rel="index"><span itemprop="name">spring</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <blockquote>
<p>转载自 <a target="_blank" rel="noopener" href="http://www.imooc.com/article/316995">http://www.imooc.com/article/316995</a></p>
</blockquote>
<p>数据的校验的重要性就不用说了，即使在前端对数据进行校验的情况下，我们还是要对传入后端的数据再进行一遍校验，避免用户绕过浏览器直接通过一些 HTTP 工具直接向后端请求一些违法数据。</p>
<p>最普通的做法就像下面这样。我们通过 <code>if/else</code> 语句对请求的每一个参数一一校验。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/api/person&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ResponseEntity&lt;PersonRequest&gt; <span class="title">save</span><span class="params">(<span class="meta">@RequestBody</span> PersonRequest personRequest)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (personRequest.getClassId() == <span class="keyword">null</span></span><br><span class="line">                || personRequest.getName() == <span class="keyword">null</span></span><br><span class="line">                || !Pattern.matches(<span class="string">&quot;(^Man$|^Woman$|^UGM$)&quot;</span>, personRequest.getSex())) &#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ResponseEntity.ok().body(personRequest);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样的代码，小伙伴们在日常开发中一定不少见，很多开源项目都是这样对请求入参做校验的。</p>
<p>但是，不太建议这样来写，这样的代码明显违背了 <strong>单一职责原则</strong>。大量的非业务代码混杂在业务代码中，非常难以维护，还会导致业务层代码冗杂！</p>
<p>实际上，我们是可以通过一些简单的手段对上面的代码进行改进的！这也是本文主要要介绍的内容！</p>
<p>废话不多说！下面我会结合自己在项目中的实际使用经验，通过实例程序演示如何在 SpringBoot 程序中优雅地的进行参数验证(普通的 Java 程序同样适用)。</p>
<p>不了解的朋友一定要好好看一下，学完马上就可以实践到项目上去。</p>
<p>并且，本文示例项目使用的是目前最新的 Spring Boot 版本 2.4.5!（截止到 2021-04-21）</p>
<p>示例项目源代码地址：github.com/CodingDocs/springboot-guide/tree/master/source-code/bean-validation-demo</p>
<h2 id="添加相关依赖"><a href="#添加相关依赖" class="headerlink" title="添加相关依赖"></a>添加相关依赖</h2><p>如果开发普通 Java 程序的的话，你需要可能需要像下面这样依赖：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.hibernate.validator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>hibernate-validator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>6.0.9.Final<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>javax.el<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javax.el-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.glassfish.web<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>javax.el<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.2.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>不过，相信大家都是使用的 Spring Boot 框架来做开发。</p>
<p>基于 Spring Boot 的话，就比较简单了，只需要给项目添加上 <code>spring-boot-starter-web</code> 依赖就够了，它的子依赖包含了我们所需要的东西。另外，我们的示例项目中还使用到了 Lombok。</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/608807950001a11805540409-20210608162556698.jpg" alt="img"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">optional</span>&gt;</span>true<span class="tag">&lt;/<span class="name">optional</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.13.1<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>但是！！！ Spring Boot 2.3 1 之后，<code>spring-boot-starter-validation</code> 已经不包括在了 <code>spring-boot-starter-web</code> 中，需要我们手动加上！</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/608807970001718b19801193-20210608162556753.jpg" alt="img"></p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-validation<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="验证-Controller-的输入"><a href="#验证-Controller-的输入" class="headerlink" title="验证 Controller 的输入"></a>验证 Controller 的输入</h2><h3 id="验证请求体"><a href="#验证请求体" class="headerlink" title="验证请求体"></a>验证请求体</h3><p>验证请求体即使验证被 <code>@RequestBody</code> 注解标记的方法参数。</p>
<p><strong><code>PersonController</code></strong></p>
<p>我们在需要验证的参数上加上了<code>@Valid</code>注解，如果验证失败，它将抛出<code>MethodArgumentNotValidException</code>。默认情况下，Spring 会将此异常转换为 HTTP Status 400（错误请求）。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/api/person&quot;)</span></span><br><span class="line"><span class="meta">@Validated</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PostMapping</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ResponseEntity&lt;PersonRequest&gt; <span class="title">save</span><span class="params">(<span class="meta">@RequestBody</span> <span class="meta">@Valid</span> PersonRequest personRequest)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ResponseEntity.ok().body(personRequest);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong><code>PersonRequest</code></strong></p>
<p>我们使用校验注解对请求的参数进行校验！</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@Builder</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonRequest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@NotNull(message = &quot;classId 不能为空&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String classId;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Size(max = 33)</span></span><br><span class="line">    <span class="meta">@NotNull(message = &quot;name 不能为空&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Pattern(regexp = &quot;(^Man$|^Woman$|^UGM$)&quot;, message = &quot;sex 值不在可选范围&quot;)</span></span><br><span class="line">    <span class="meta">@NotNull(message = &quot;sex 不能为空&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String sex;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>正则表达式说明：</p>
<ul>
<li><code>^string</code> : 匹配以 string 开头的字符串</li>
<li><code>string$</code> ：匹配以 string 结尾的字符串</li>
<li><code>^string$</code> ：精确匹配 string 字符串</li>
<li><code>(^Man$|^Woman$|^UGM$)</code> : 值只能在 Man,Woman,UGM 这三个值中选择</li>
</ul>
<p><strong><code>GlobalExceptionHandler</code></strong></p>
<p>自定义异常处理器可以帮助我们捕获异常，并进行一些简单的处理。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ControllerAdvice(assignableTypes = &#123;PersonController.class&#125;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GlobalExceptionHandler</span> </span>&#123;</span><br><span class="line">    <span class="meta">@ExceptionHandler(MethodArgumentNotValidException.class)</span></span><br><span class="line">    <span class="keyword">public</span> ResponseEntity&lt;Map&lt;String, String&gt;&gt; handleValidationExceptions(</span><br><span class="line">            MethodArgumentNotValidException ex) &#123;</span><br><span class="line">        Map&lt;String, String&gt; errors = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        ex.getBindingResult().getAllErrors().forEach((error) -&gt; &#123;</span><br><span class="line">            String fieldName = ((FieldError) error).getField();</span><br><span class="line">            String errorMessage = error.getDefaultMessage();</span><br><span class="line">            errors.put(fieldName, errorMessage);</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="keyword">return</span> ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>通过测试验证</strong></p>
<p>下面我通过 <code>MockMvc</code> 模拟请求 <code>Controller</code> 的方式来验证是否生效。当然了，你也可以通过 <code>Postman</code> 这种工具来验证。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="meta">@AutoConfigureMockMvc</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonControllerTest</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> MockMvc mockMvc;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ObjectMapper objectMapper;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 验证出现参数不合法的情况抛出异常并且可以正确被捕获</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">should_check_person_value</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        PersonRequest personRequest = PersonRequest.builder().sex(<span class="string">&quot;Man22&quot;</span>)</span><br><span class="line">                .classId(<span class="string">&quot;82938390&quot;</span>).build();</span><br><span class="line">        mockMvc.perform(post(<span class="string">&quot;/api/personRequest&quot;</span>)</span><br><span class="line">                .contentType(MediaType.APPLICATION_JSON)</span><br><span class="line">                .content(objectMapper.writeValueAsString(personRequest)))</span><br><span class="line">                .andExpect(MockMvcResultMatchers.jsonPath(<span class="string">&quot;sex&quot;</span>).value(<span class="string">&quot;sex 值不在可选范围&quot;</span>))</span><br><span class="line">                .andExpect(MockMvcResultMatchers.jsonPath(<span class="string">&quot;name&quot;</span>).value(<span class="string">&quot;name 不能为空&quot;</span>));</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>使用 <code>Postman</code> 验证</strong></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/608807980001efe108610589-20210608162556897.jpg" alt="img"></p>
<h3 id="验证请求参数"><a href="#验证请求参数" class="headerlink" title="验证请求参数"></a>验证请求参数</h3><p>验证请求参数（Path Variables 和 Request Parameters）即是验证被 <code>@PathVariable</code> 以及 <code>@RequestParam</code> 标记的方法参数。</p>
<p><strong><code>PersonController</code></strong></p>
<p><strong>一定一定不要忘记在类上加上 <code>Validated</code> 注解了，这个参数可以告诉 Spring 去校验方法参数。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/api/persons&quot;)</span></span><br><span class="line"><span class="meta">@Validated</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/&#123;id&#125;&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ResponseEntity&lt;Integer&gt; <span class="title">getPersonByID</span><span class="params">(<span class="meta">@Valid</span> <span class="meta">@PathVariable(&quot;id&quot;)</span> <span class="meta">@Max(value = 5, message = &quot;超过 id 的范围了&quot;)</span> Integer id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ResponseEntity.ok().body(id);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@PutMapping</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ResponseEntity&lt;String&gt; <span class="title">getPersonByName</span><span class="params">(<span class="meta">@Valid</span> <span class="meta">@RequestParam(&quot;name&quot;)</span> <span class="meta">@Size(max = 6, message = &quot;超过 name 的范围了&quot;)</span> String name)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> ResponseEntity.ok().body(name);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong><code>ExceptionHandler</code></strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@ExceptionHandler(ConstraintViolationException.class)</span></span><br><span class="line"><span class="function">ResponseEntity&lt;String&gt; <span class="title">handleConstraintViolationException</span><span class="params">(ConstraintViolationException e)</span> </span>&#123;</span><br><span class="line">   <span class="keyword">return</span> ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>通过测试验证</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">should_check_path_variable</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    mockMvc.perform(get(<span class="string">&quot;/api/person/6&quot;</span>)</span><br><span class="line">                    .contentType(MediaType.APPLICATION_JSON))</span><br><span class="line">      .andExpect(status().isBadRequest())</span><br><span class="line">      .andExpect(content().string(<span class="string">&quot;getPersonByID.id: 超过 id 的范围了&quot;</span>));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">should_check_request_param_value2</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    mockMvc.perform(put(<span class="string">&quot;/api/person&quot;</span>)</span><br><span class="line">                    .param(<span class="string">&quot;name&quot;</span>, <span class="string">&quot;snailclimbsnailclimb&quot;</span>)</span><br><span class="line">                    .contentType(MediaType.APPLICATION_JSON))</span><br><span class="line">      .andExpect(status().isBadRequest())</span><br><span class="line">      .andExpect(content().string(<span class="string">&quot;getPersonByName.name: 超过 name 的范围了&quot;</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>使用 <code>Postman</code> 验证</strong></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/6088079800014eab08330472-20210608162556919.jpg" alt="img"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/608807990001847608740443-20210608162557043.jpg" alt="img"></p>
<h2 id="验证-Service-中的方法"><a href="#验证-Service-中的方法" class="headerlink" title="验证 Service 中的方法"></a>验证 Service 中的方法</h2><p>我们还可以验证任何 Spring Bean 的输入，而不仅仅是 <code>Controller</code> 级别的输入。通过使用<code>@Validated</code>和<code>@Valid</code>注释的组合即可实现这一需求！</p>
<p>一般情况下，我们在项目中也更倾向于使用这种方案。</p>
<p><strong>一定一定不要忘记在类上加上 <code>Validated</code> 注解了，这个参数可以告诉 Spring 去校验方法参数。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@Validated</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">validatePersonRequest</span><span class="params">(<span class="meta">@Valid</span> PersonRequest personRequest)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// do something</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>通过测试验证：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringRunner.class)</span></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonServiceTest</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> PersonService service;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">should_throw_exception_when_person_request_is_not_valid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            PersonRequest personRequest = PersonRequest.builder().sex(<span class="string">&quot;Man22&quot;</span>)</span><br><span class="line">                    .classId(<span class="string">&quot;82938390&quot;</span>).build();</span><br><span class="line">            service.validatePersonRequest(personRequest);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ConstraintViolationException e) &#123;</span><br><span class="line">           <span class="comment">// 输出异常信息</span></span><br><span class="line">            e.getConstraintViolations().forEach(constraintViolation -&gt; System.out.println(constraintViolation.getMessage()));</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">name 不能为空</span><br><span class="line">sex 值不在可选范围</span><br></pre></td></tr></table></figure>

<h2 id="Validator-编程方式手动进行参数验证"><a href="#Validator-编程方式手动进行参数验证" class="headerlink" title="Validator 编程方式手动进行参数验证"></a>Validator 编程方式手动进行参数验证</h2><p>某些场景下可能会需要我们手动校验并获得校验结果。</p>
<p>我们通过 <code>Validator</code> 工厂类获得的 <code>Validator</code> 示例。另外，如果是在 Spring Bean 中的话，还可以通过 <code>@Autowired</code> 直接注入的方式。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line">Validator validate</span><br></pre></td></tr></table></figure>

<p>具体使用情况如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">ValidatorFactory factory = Validation.buildDefaultValidatorFactory();</span><br><span class="line">Validator validator = factory.getValidator()</span><br><span class="line">PersonRequest personRequest = PersonRequest.builder().sex(<span class="string">&quot;Man22&quot;</span>)</span><br><span class="line">  .classId(<span class="string">&quot;82938390&quot;</span>).build();</span><br><span class="line">Set&lt;ConstraintViolation&lt;PersonRequest&gt;&gt; violations = validator.validate(personRequest);</span><br><span class="line"><span class="comment">// 输出异常信息</span></span><br><span class="line">violations.forEach(constraintViolation -&gt; System.out.println(constraintViolation.getMessage()));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>输出结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sex 值不在可选范围</span><br><span class="line">name 不能为空</span><br></pre></td></tr></table></figure>

<h2 id="自定以-Validator-实用"><a href="#自定以-Validator-实用" class="headerlink" title="自定以 Validator(实用)"></a>自定以 Validator(实用)</h2><p>如果自带的校验注解无法满足你的需求的话，你还可以自定义实现注解。</p>
<h3 id="案例一-校验特定字段的值是否在可选范围"><a href="#案例一-校验特定字段的值是否在可选范围" class="headerlink" title="案例一:校验特定字段的值是否在可选范围"></a>案例一:校验特定字段的值是否在可选范围</h3><p>比如我们现在多了这样一个需求：<code>PersonRequest</code> 类多了一个 <code>Region</code> 字段，<code>Region</code> 字段只能是<code>China</code>、<code>China-Taiwan</code>、<code>China-HongKong</code>这三个中的一个。</p>
<p><strong>第一步，你需要创建一个注解 <code>Region</code>。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Target(&#123;FIELD&#125;)</span></span><br><span class="line"><span class="meta">@Retention(RUNTIME)</span></span><br><span class="line"><span class="meta">@Constraint(validatedBy = RegionValidator.class)</span></span><br><span class="line"><span class="meta">@Documented</span></span><br><span class="line"><span class="keyword">public</span> <span class="meta">@interface</span> Region &#123;</span><br><span class="line"></span><br><span class="line">    <span class="function">String <span class="title">message</span><span class="params">()</span> <span class="keyword">default</span> &quot;Region 值不在可选范围内&quot;</span>;</span><br><span class="line"></span><br><span class="line">    Class&lt;?&gt;[] groups() <span class="keyword">default</span> &#123;&#125;;</span><br><span class="line"></span><br><span class="line">    Class&lt;? extends Payload&gt;[] payload() <span class="keyword">default</span> &#123;&#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>第二步，你需要实现 <code>ConstraintValidator</code>接口，并重写<code>isValid</code> 方法。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RegionValidator</span> <span class="keyword">implements</span> <span class="title">ConstraintValidator</span>&lt;<span class="title">Region</span>, <span class="title">String</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">isValid</span><span class="params">(String value, ConstraintValidatorContext context)</span> </span>&#123;</span><br><span class="line">        HashSet&lt;Object&gt; regions = <span class="keyword">new</span> HashSet&lt;&gt;();</span><br><span class="line">        regions.add(<span class="string">&quot;China&quot;</span>);</span><br><span class="line">        regions.add(<span class="string">&quot;China-Taiwan&quot;</span>);</span><br><span class="line">        regions.add(<span class="string">&quot;China-HongKong&quot;</span>);</span><br><span class="line">        <span class="keyword">return</span> regions.contains(value);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在你就可以使用这个注解：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Region</span></span><br><span class="line"><span class="keyword">private</span> String region;</span><br></pre></td></tr></table></figure>

<p><strong>通过测试验证</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PersonRequest personRequest = PersonRequest.builder()</span><br><span class="line"> 	 .region(<span class="string">&quot;Shanghai&quot;</span>).build();</span><br><span class="line">mockMvc.perform(post(<span class="string">&quot;/api/person&quot;</span>)</span><br><span class="line">                .contentType(MediaType.APPLICATION_JSON)</span><br><span class="line">                .content(objectMapper.writeValueAsString(personRequest)))</span><br><span class="line">  .andExpect(MockMvcResultMatchers.jsonPath(<span class="string">&quot;region&quot;</span>).value(<span class="string">&quot;Region 值不在可选范围内&quot;</span>));</span><br></pre></td></tr></table></figure>

<p><strong>使用 <code>Postman</code> 验证</strong></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/6088079a0001ae5808670574-20210608162557178.jpg" alt="img"></p>
<h3 id="案例二-校验电话号码"><a href="#案例二-校验电话号码" class="headerlink" title="案例二:校验电话号码"></a>案例二:校验电话号码</h3><p>校验我们的电话号码是否合法，这个可以通过正则表达式来做，相关的正则表达式都可以在网上搜到，你甚至可以搜索到针对特定运营商电话号码段的正则表达式。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">PhoneNumber.java</span><br><span class="line">@Documented</span><br><span class="line">@Constraint(validatedBy &#x3D; PhoneNumberValidator.class)</span><br><span class="line">@Target(&#123;FIELD, PARAMETER&#125;)</span><br><span class="line">@Retention(RUNTIME)</span><br><span class="line">public @interface PhoneNumber &#123;</span><br><span class="line">    String message() default &quot;Invalid phone number&quot;;</span><br><span class="line">    Class[] groups() default &#123;&#125;;</span><br><span class="line">    Class[] payload() default &#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line">PhoneNumberValidator.java</span><br><span class="line">public class PhoneNumberValidator implements ConstraintValidator&lt;PhoneNumber, String&gt; &#123;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public boolean isValid(String phoneField, ConstraintValidatorContext context) &#123;</span><br><span class="line">        if (phoneField &#x3D;&#x3D; null) &#123;</span><br><span class="line">            &#x2F;&#x2F; can be null</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        &#x2F;&#x2F;  大陆手机号码11位数，匹配格式：前三位固定格式+后8位任意数</span><br><span class="line">        &#x2F;&#x2F; ^ 匹配输入字符串开始的位置</span><br><span class="line">        &#x2F;&#x2F; \d 匹配一个或多个数字，其中 \ 要转义，所以是 \\d</span><br><span class="line">        &#x2F;&#x2F; $ 匹配输入字符串结尾的位置</span><br><span class="line">        String regExp &#x3D; &quot;^[1]((3[0-9])|(4[5-9])|(5[0-3,5-9])|([6][5,6])|(7[0-9])|(8[0-9])|(9[1,8,9]))\\d&#123;8&#125;$&quot;;</span><br><span class="line">        return phoneField.matches(regExp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>搞定，我们现在就可以使用这个注解了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@PhoneNumber(message = &quot;phoneNumber 格式不正确&quot;)</span></span><br><span class="line"><span class="meta">@NotNull(message = &quot;phoneNumber 不能为空&quot;)</span></span><br><span class="line"><span class="keyword">private</span> String phoneNumber;</span><br></pre></td></tr></table></figure>

<p><strong>通过测试验证</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">PersonRequest personRequest = PersonRequest.builder()</span><br><span class="line">  	.phoneNumber(<span class="string">&quot;1816313815&quot;</span>).build();</span><br><span class="line">mockMvc.perform(post(<span class="string">&quot;/api/person&quot;</span>)</span><br><span class="line">                .contentType(MediaType.APPLICATION_JSON)</span><br><span class="line">                .content(objectMapper.writeValueAsString(personRequest)))</span><br><span class="line">  .andExpect(MockMvcResultMatchers.jsonPath(<span class="string">&quot;phoneNumber&quot;</span>).value(<span class="string">&quot;phoneNumber 格式不正确&quot;</span>));</span><br></pre></td></tr></table></figure>

<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/6088079a00019ccb08820559-20210608162557277.jpg" alt="img"></p>
<h2 id="使用验证组"><a href="#使用验证组" class="headerlink" title="使用验证组"></a>使用验证组</h2><p>验证组我们基本是不会用到的，也不太建议在项目中使用，理解起来比较麻烦，写起来也比较麻烦。简单了解即可！</p>
<p>当我们对对象操作的不同方法有不同的验证规则的时候才会用到验证组。</p>
<p>我写一个简单的例子，你们就能看明白了！</p>
<p><strong>1.先创建两个接口，代表不同的验证组</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AddPersonGroup</span> </span>&#123;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DeletePersonGroup</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>2.使用验证组</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Person</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 当验证组为 DeletePersonGroup 的时候 group 字段不能为空</span></span><br><span class="line">    <span class="meta">@NotNull(groups = DeletePersonGroup.class)</span></span><br><span class="line">    <span class="comment">// 当验证组为 AddPersonGroup 的时候 group 字段需要为空</span></span><br><span class="line">    <span class="meta">@Null(groups = AddPersonGroup.class)</span></span><br><span class="line">    <span class="keyword">private</span> String group;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="meta">@Validated</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Validated(AddPersonGroup.class)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">validatePersonGroupForAdd</span><span class="params">(<span class="meta">@Valid</span> Person person)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// do something</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Validated(DeletePersonGroup.class)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">validatePersonGroupForDelete</span><span class="params">(<span class="meta">@Valid</span> Person person)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// do something</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过测试验证：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test(expected = ConstraintViolationException.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">should_check_person_with_groups</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Person person = <span class="keyword">new</span> Person();</span><br><span class="line">    person.setGroup(<span class="string">&quot;group1&quot;</span>);</span><br><span class="line">    service.validatePersonGroupForAdd(person);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Test(expected = ConstraintViolationException.class)</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">should_check_person_with_groups2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Person person = <span class="keyword">new</span> Person();</span><br><span class="line">    service.validatePersonGroupForDelete(person);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>验证组使用下来的体验就是有点反模式的感觉，让代码的可维护性变差了！尽量不要使用！</p>
<h2 id="常用校验注解总结"><a href="#常用校验注解总结" class="headerlink" title="常用校验注解总结"></a>常用校验注解总结</h2><p><code>JSR303</code> 定义了 <code>Bean Validation</code>（校验）的标准 <code>validation-api</code>，并没有提供实现。<code>Hibernate Validation</code>是对这个规范/规范的实现 <code>hibernate-validator</code>，并且增加了 <code>@Email</code>、<code>@Length</code>、<code>@Range</code> 等注解。<code>Spring Validation</code> 底层依赖的就是<code>Hibernate Validation</code>。</p>
<p><strong>JSR 提供的校验注解</strong>:</p>
<ul>
<li><code>@Null</code> 被注释的元素必须为 null</li>
<li><code>@NotNull</code> 被注释的元素必须不为 null</li>
<li><code>@AssertTrue</code> 被注释的元素必须为 true</li>
<li><code>@AssertFalse</code> 被注释的元素必须为 false</li>
<li><code>@Min(value)</code> 被注释的元素必须是一个数字，其值必须大于等于指定的最小值</li>
<li><code>@Max(value)</code> 被注释的元素必须是一个数字，其值必须小于等于指定的最大值</li>
<li><code>@DecimalMin(value)</code> 被注释的元素必须是一个数字，其值必须大于等于指定的最小值</li>
<li><code>@DecimalMax(value)</code> 被注释的元素必须是一个数字，其值必须小于等于指定的最大值</li>
<li><code>@Size(max=, min=)</code> 被注释的元素的大小必须在指定的范围内</li>
<li><code>@Digits (integer, fraction)</code> 被注释的元素必须是一个数字，其值必须在可接受的范围内</li>
<li><code>@Past</code> 被注释的元素必须是一个过去的日期</li>
<li><code>@Future</code> 被注释的元素必须是一个将来的日期</li>
<li><code>@Pattern(regex=,flag=)</code> 被注释的元素必须符合指定的正则表达式</li>
</ul>
<p><strong>Hibernate Validator 提供的校验注解</strong>：</p>
<ul>
<li><code>@NotBlank(message =)</code> 验证字符串非 null，且长度必须大于 0</li>
<li><code>@Email</code> 被注释的元素必须是电子邮箱地址</li>
<li><code>@Length(min=,max=)</code> 被注释的字符串的大小必须在指定的范围内</li>
<li><code>@NotEmpty</code> 被注释的字符串的必须非空</li>
<li><code>@Range(min=,max=,message=)</code> 被注释的元素必须在合适的范围内</li>
</ul>
<h2 id="拓展"><a href="#拓展" class="headerlink" title="拓展"></a>拓展</h2><p>经常有小伙伴问到：“<code>@NotNull</code> 和 <code>@Column(nullable = false)</code> 两者有什么区别？”</p>
<p>我这里简单回答一下：</p>
<ul>
<li><code>@NotNull</code>是 JSR 303 Bean 验证批注,它与数据库约束本身无关。</li>
<li><code>@Column(nullable = false)</code> : 是 JPA 声明列为非空的方法。</li>
</ul>
<p>总结来说就是即前者用于验证，而后者则用于指示数据库创建表的时候对表的约束。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/02/%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9%E5%9E%82%E7%9B%B4%E5%88%87%E5%88%86%E3%80%81%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/02/%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9%E5%9E%82%E7%9B%B4%E5%88%87%E5%88%86%E3%80%81%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86/" class="post-title-link" itemprop="url">如何选择垂直切分、水平切分</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-06-02 22:18:22" itemprop="dateCreated datePublished" datetime="2021-06-02T22:18:22+08:00">2021-06-02</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-06-09 20:44:05" itemprop="dateModified" datetime="2021-06-09T20:44:05+08:00">2021-06-09</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/mycat/" itemprop="url" rel="index"><span itemprop="name">mycat</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210609203437856.png" alt="image-20210609203437856"></p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/06/02/%E5%A6%82%E4%BD%95%E9%80%89%E6%8B%A9%E5%9E%82%E7%9B%B4%E5%88%87%E5%88%86%E3%80%81%E6%B0%B4%E5%B9%B3%E5%88%87%E5%88%86/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/02/SpringBoot%E9%9B%86%E6%88%90jasypt%E5%AE%9E%E7%8E%B0%E9%85%8D%E7%BD%AE%E5%8A%A0%E5%AF%86/" class="post-title-link" itemprop="url">SpringBoot集成jasypt实现配置加密</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2021-06-02 19:32:08 / 修改时间：19:46:56" itemprop="dateCreated datePublished" datetime="2021-06-02T19:32:08+08:00">2021-06-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/java-spring/" itemprop="url" rel="index"><span itemprop="name">java/spring</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <blockquote>
<p>转载自 <a target="_blank" rel="noopener" href="https://blog.lqdev.cn/2019/05/08/springboot/chapter-thirty-seven/">https://blog.lqdev.cn/2019/05/08/springboot/chapter-thirty-seven/</a></p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><blockquote>
<p>近期在进行项目安全方面评审时，质量管理部门有提出需要对配置文件中的敏高文件进行加密处理，避免了信息泄露问题。想想前段时间某公司上传github时，把相应的生产数据库明文密码也一并上传了，导致了相应的数据泄露问题。也确实，大部分项目无论开发、测试还是生产环境，相关的敏高信息都是明文存储的，也是一大安全隐患呀。所以今天来说说，如何对配置文件进行加密操作。</p>
</blockquote>
<h2 id="一点知识"><a href="#一点知识" class="headerlink" title="一点知识"></a>一点知识</h2><h3 id="何为Jasypt"><a href="#何为Jasypt" class="headerlink" title="何为Jasypt"></a>何为Jasypt</h3><blockquote>
<p><a target="_blank" rel="noopener" href="http://jasypt.org/">Jasypt</a>是一个Java库，允许开发人员以很简单的方式添加基本加密功能，而无需深入研究加密原理。利用它可以实现高安全性的，基于标准的加密技术，无论是单向和双向加密。加密密码，文本，数字，二进制文件。</p>
</blockquote>
<ol>
<li>高安全性的，基于标准的加密技术，无论是单向和双向加密。加密密码，文本，数字，二进制文件…</li>
<li>集成Hibernate的。</li>
<li>可集成到Spring应用程序中，与Spring Security集成。</li>
<li>集成的能力，用于加密的应用程序（即数据源）的配置。</li>
<li>特定功能的高性能加密的multi-processor/multi-core系统。</li>
<li>与任何JCE提供者使用开放的API</li>
</ol>
<p>官网：<a target="_blank" rel="noopener" href="http://www.jasypt.org/">http://www.jasypt.org/</a></p>
<h2 id="SpringBoot集成Jasypt"><a href="#SpringBoot集成Jasypt" class="headerlink" title="SpringBoot集成Jasypt"></a>SpringBoot集成Jasypt</h2><blockquote>
<p><code>SpringBoot</code>中集成<code>Jasypt</code>，可直接使用开源的<code>jasypt-spring-boot</code>直接集成，使用简单方便。</p>
</blockquote>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/WTvOjzNR7Xt5.png" alt="mark"></p>
<h3 id="常规集成示例"><a href="#常规集成示例" class="headerlink" title="常规集成示例"></a>常规集成示例</h3><ol>
<li>引入pom依赖</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<ol start="2">
<li>设置盐值和修改相应需要加密的配置参数</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"># 需要解密的地方，使用ENC()进行包裹处理</span><br><span class="line">okong.name&#x3D;ENC(Xj7Ykn2O0Hni&#x2F;tN4oojPfw&#x3D;&#x3D;)</span><br><span class="line"></span><br><span class="line"># 设置盐值，生产环境中，切记不要直接进行设置，可通过环境变量、命令行等形式进行设置。</span><br><span class="line">jasypt.encryptor.password&#x3D;lqdev</span><br></pre></td></tr></table></figure>

<p>简单来说，就是在需要加密的值使用<code>ENC(</code>和<code>)</code>进行包裹，即：<code>ENC(密文)</code>。若想避免参数冲突，可修改前缀和后缀，可以直接使用<code>jasypt.encryptor.property.prefix</code>和<code>jasypt.encryptor.property.suffix</code>进行修改即可。</p>
<p><strong>之后想往常一样使用<code>@Value(&quot;$&#123;&#125;&quot;)</code>即可。</strong></p>
<h3 id="包含xml引入时"><a href="#包含xml引入时" class="headerlink" title="包含xml引入时"></a>包含xml引入时</h3><blockquote>
<p>在一些使用<code>javaBean</code>配置和<code>xml</code>两种混合模式时，使用第一种配置时，<code>xml</code>参数并未替换。此时看了官方文档，可以使用另一方式进行配置即可。</p>
</blockquote>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/bPoPUpE8uk4A.png" alt="官方说明"></p>
<ol>
<li><p>引入pom依赖</p>
 <figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.github.ulisesbocchio<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>jasypt-spring-boot<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.18<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p> 其实就是不进行自动配置而已。</p>
</li>
<li><p>启动类启动方式修改。</p>
 <figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@Slf4j</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JasyptApplication</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// SpringApplication.run(JasyptApplication.class, args);</span></span><br><span class="line">    <span class="comment">// 使用自定义环境变量 实现一些特殊场景下的加密字符解密操作</span></span><br><span class="line">    <span class="comment">// 若无额外的xml引入文件需要解密时，可直接使用SpringApplication.run(JasyptApplication.class, args);即可</span></span><br><span class="line">    <span class="comment">// 若想在引入的xml中使用，需要加入环境变量，如以下模式</span></span><br><span class="line">    <span class="keyword">new</span> SpringApplicationBuilder().environment(<span class="keyword">new</span> StandardEncryptableEnvironment())</span><br><span class="line">    .sources(JasyptApplication.class).run(args);</span><br><span class="line">    log.info(<span class="string">&quot;spring-boot-jasypt-chapter37服务启动!&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="其他配置项"><a href="#其他配置项" class="headerlink" title="其他配置项"></a>其他配置项</h3><table>
<thead>
<tr>
<th>Key</th>
<th>Required</th>
<th>Default Value</th>
</tr>
</thead>
<tbody><tr>
<td>jasypt.encryptor.password</td>
<td><strong>True</strong></td>
<td>盐值，根密码</td>
</tr>
<tr>
<td>jasypt.encryptor.algorithm</td>
<td>False</td>
<td>PBEWithMD5AndDES</td>
</tr>
<tr>
<td>jasypt.encryptor.keyObtentionIterations</td>
<td>False</td>
<td>1000</td>
</tr>
<tr>
<td>jasypt.encryptor.poolSize</td>
<td>False</td>
<td>1</td>
</tr>
<tr>
<td>jasypt.encryptor.providerName</td>
<td>False</td>
<td>SunJCE</td>
</tr>
<tr>
<td>jasypt.encryptor.providerClassName</td>
<td>False</td>
<td>null</td>
</tr>
<tr>
<td>jasypt.encryptor.saltGeneratorClassname</td>
<td>False</td>
<td>org.jasypt.salt.RandomSaltGenerator</td>
</tr>
<tr>
<td>jasypt.encryptor.ivGeneratorClassname</td>
<td>False</td>
<td>org.jasypt.salt.NoOpIVGenerator</td>
</tr>
<tr>
<td>jasypt.encryptor.stringOutputType</td>
<td>False</td>
<td>base64</td>
</tr>
<tr>
<td>jasypt.encryptor.proxyPropertySources</td>
<td>False</td>
<td>false</td>
</tr>
</tbody></table>
<h2 id="运维说明"><a href="#运维说明" class="headerlink" title="运维说明"></a>运维说明</h2><p>为了方便运维人员对各类敏感密钥进行加密操作，提供了自动化脚本，方便生成相应的加密串。</p>
<h3 id="密钥（盐值）存储说明"><a href="#密钥（盐值）存储说明" class="headerlink" title="密钥（盐值）存储说明"></a>密钥（盐值）存储说明</h3><p>本身加解密过程都是通过<code>盐值</code>进行处理的，所以正常情况下<code>盐值</code>和<code>加密串</code>是分开存储的。**<code>盐值</code>应该放在<code>系统属性</code>、<code>命令行</code>或是<code>环境变量</code>来使用，而不是放在配置文件。**</p>
<h4 id="命令行示例"><a href="#命令行示例" class="headerlink" title="命令行示例"></a>命令行示例</h4><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar xxx.jar --jasypt.encryptor.password=xxx &amp;</span><br></pre></td></tr></table></figure>

<h4 id="环境变量示例"><a href="#环境变量示例" class="headerlink" title="环境变量示例"></a>环境变量示例</h4><p>设置环境变量：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim /etc/profileexport JASYPT_PASSWORD = xxxx</span><br></pre></td></tr></table></figure>

<p>启动命令：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -jar xxx.jar --jasypt.encryptor.password=<span class="variable">$&#123;JASYPT_PASSWORD&#125;</span> &amp;</span><br></pre></td></tr></table></figure>

<h3 id="bat脚本"><a href="#bat脚本" class="headerlink" title="bat脚本"></a>bat脚本</h3><p>为了方便，简单编写了一个bat脚本方便使用。</p>
<figure class="highlight bat"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">@<span class="built_in">echo</span> off</span><br><span class="line"><span class="built_in">set</span>/p input=待加密的明文字符串：</span><br><span class="line"><span class="built_in">set</span>/p password=加密密钥(盐值)：</span><br><span class="line"><span class="built_in">echo</span> 加密中......</span><br><span class="line">java -cp jasypt-<span class="number">1</span>.<span class="number">9</span>.<span class="number">2</span>.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI  input=<span class="variable">%input%</span> password=<span class="variable">%password%</span> algorithm=PBEWithMD5AndDES</span><br><span class="line"><span class="built_in">pause</span></span><br></pre></td></tr></table></figure>

<p><strong>注意：<code>jasypt-1.9.2.jar</code> 文件需要和bat脚本放在相同目录下。此包可直接在示例项目中直接下载。</strong></p>
<p>使用示例：</p>
<p><strong>注意：相应加密串，每次加密的结果是不同的。</strong></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/568wfkAWA5Tl.png" alt="使用示例"></p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>本章节主要简单介绍了如何使用<code>jasypt</code>对配置文件进行加密操作。一些其他高级应用，可以查看官方文档进行相关集成即可。集成起来相对来说比较简单，注意是要对<code>密码(盐值)</code>的管理，需要进行安全把控下，建议运维人员针对每个项目进行不一样的盐值操作，避免一个项目泄露了，造成其他关联项的信息泄露。安全无大小呀，还是谨慎为妙！</p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><ol>
<li><a target="_blank" rel="noopener" href="https://github.com/ulisesbocchio/jasypt-spring-boot">https://github.com/ulisesbocchio/jasypt-spring-boot</a></li>
<li><a target="_blank" rel="noopener" href="http://www.jasypt.org/">http://www.jasypt.org/</a></li>
</ol>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/" class="post-title-link" itemprop="url">MyCat概述与基本概念</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-06-01 13:00:02" itemprop="dateCreated datePublished" datetime="2021-06-01T13:00:02+08:00">2021-06-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-06-05 14:39:55" itemprop="dateModified" datetime="2021-06-05T14:39:55+08:00">2021-06-05</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8/" itemprop="url" rel="index"><span itemprop="name">分库分表</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="什么是MyCat"><a href="#什么是MyCat" class="headerlink" title="什么是MyCat"></a>什么是MyCat</h2><p>MyCat 是什么？从定义和分类来看，它是一个开源的分布式数据库系统，前端的用户可以把它看成一个数据库代理，用MySql客户端和命令行工具都可以访问，而其后端则是用MySql原生的协议与多个MySql服务之间进行通信。MyCat的核心功能是分库分表，即将一个大表水平切分成N个小表，然后存放在后端的MySql数据当中。</p>
<p>MyCat发展到目前的版本，已经不是一个单纯的MySql代理了，它的后端支持MySql，Oracle，SqlServer，DB2等主流的数据库，也支持MongoDB这种NoSql数据库。而对于前端的用户来说，无论后端采用哪一种数据库，在MyCat里都是一个传统的数据库，支持标准的SQL语句，对于前端的开发人员来说，可以大大地降低开发难度，提升开发速度。</p>
          <!--noindex-->
            <div class="post-button">
              <a class="btn" href="/2021/06/01/MyCat%E6%A6%82%E8%BF%B0%E4%B8%8E%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5/#more" rel="contents">
                阅读全文 &raquo;
              </a>
            </div>
          <!--/noindex-->
        
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/5/"><i class="fa fa-angle-left" aria-label="上一页"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/5/">5</a><span class="page-number current">6</span><a class="page-number" href="/page/7/">7</a><span class="space">&hellip;</span><a class="page-number" href="/page/24/">24</a><a class="extend next" rel="next" href="/page/7/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
